package io.github.rainsoil.fastboot.system.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.github.rainsoil.fastboot.common.core.PageInfo;
import io.github.rainsoil.fastboot.common.core.PageRequest;
import io.github.rainsoil.fastboot.common.data.mybatis.PageHandler;
import io.github.rainsoil.fastboot.system.entity.SysRole;
import io.github.rainsoil.fastboot.system.service.ISysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统角色表 前端控制器
 *
 * @author luyanan
 * @since 2023-11-29
 */
@Api(value = "系统角色表", description = "系统角色表")
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/sysRole")
public class SysRoleController {

	private final ISysRoleService iSysRoleService;

	/**
	 * 分页
	 *
	 * @param pageRequest 分页参数
	 * @return 分页返回
	 * @since 2023-11-29
	 */
	@ApiOperation(value = "分页")
	@PostMapping("page")
	public PageInfo<SysRole> page(@RequestBody PageRequest<SysRole> pageRequest) {
		PageInfo<SysRole> pageInfo = iSysRoleService.page(pageRequest, new PageHandler<SysRole>() {
		});
		return pageInfo;
	}

	/**
	 * 保存
	 *
	 * @param sysRole 实体类
	 * @return 是否成功
	 * @since 2023-11-29
	 */
	@PostMapping("")
	@ApiOperation(value = "保存")
	public Boolean save(@RequestBody SysRole sysRole) {

		return this.iSysRoleService.save(sysRole);
	}

	/**
	 * 修改
	 *
	 * @param sysRole 实体类
	 * @return 是否成功
	 * @since 2023-11-29
	 */
	@ApiOperation(value = "修改")
	@PutMapping("")
	public Boolean update(@RequestBody SysRole sysRole) {
		return this.iSysRoleService.updateById(sysRole);

	}

	/**
	 * 根据id删除
	 *
	 * @param id id
	 * @return 是否成功
	 * @since 2023-11-29
	 */
	@DeleteMapping()
	@ApiOperation(value = "根据id删除")
	public Boolean remove(@RequestParam(value = "id", required = true) Long id) {
		return this.iSysRoleService.removeById(id);
	}


	/**
	 * 查询所有的角色
	 *
	 * @param roleName 角色名称
	 * @return
	 * @since 2024/01/06
	 */
	@GetMapping("choose/role")
	@ApiOperation(value = "查询所有的角色")
	public List<SysRole> list(String roleName) {

		return iSysRoleService.list(new LambdaQueryWrapper<SysRole>().like(StrUtil.isNotBlank(roleName), SysRole::getRoleName, roleName));
	}
}
